﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonInterfaces.Models;
using System.Windows.Controls;

namespace StandardScriptItems.SimpleControls
{
    public class ElementProperySelector : UserControl
    {
        public IElementState state;

        public ElementStateComboBox ElementBox;
        public EditableComboBox PropertyBox;

        public delegate void PropertyChangedDelegate(string selectedProperty, string propertyValue);
        public event PropertyChangedDelegate PropertyChanged;

        public ElementProperySelector(IElementState state)
        {
            Name = "ElementPropertySelector";
            this.state = state;
            InitializeComponents();
            
        }

        private void InitializeComponents()
        {
            Grid grid = new Grid();
            grid.Name = "Griddy";
            grid.ColumnDefinitions.Add(new ColumnDefinition());
            grid.ColumnDefinitions.Add(new ColumnDefinition());
            
            ElementBox = new ElementStateComboBox(state);
            ElementBox.ElementSelectionChanged += delegate(String element)
            {
                PropertyBox.Clear();
                (PropertyBox[-1] as ComboBoxItem).IsSelected = true;
                foreach (String prop in state.Property(element))
                {
                    PropertyBox.Add(prop);
                }
            };
            Grid.SetColumn(ElementBox, 0);
            grid.Children.Add(ElementBox);
            
            PropertyBox = new EditableComboBox("--Which property?--", new List<String>());
            PropertyBox.Name = "PropertySelector";
            PropertyBox.SelectionChanged += delegate(object sender, SelectionChangedEventArgs args)
            {
                PropertyChanged(PropertyBox.ToString(), state.ValueOf(PropertyBox.ToString(), ElementBox.ToString()));
            };
            Grid.SetColumn(PropertyBox, 1);
            grid.Children.Add(PropertyBox);

            this.Content = grid;
        }

        public override string ToString()
        {
            String[] split = PropertyBox.ToString().Split(new char[] { '.' }, 2);
            if (split.Length == 2)
                return ElementBox.ToString() + " | " + split[0] + " | ." + split[1];
            return ElementBox.ToString() + " | " + split[0] + " | ";
        }
    }
}
